<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="productName">
        <el-input
            v-model="queryParams.productName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="sku" prop="sku">
        <el-input
            v-model="queryParams.sku"
            placeholder="请输入sku"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-input
            v-model="queryParams.category"
            placeholder="请输入分类"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
            v-model="queryParams.brand"
            placeholder="请输入品牌"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品图标地址" prop="imageUrl">
        <el-input
            v-model="queryParams.imageUrl"
            placeholder="请输入商品图标地址"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品原价" prop="originalPrice">
        <el-input
            v-model="queryParams.originalPrice"
            placeholder="请输入商品原价"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售价" prop="salePrice">
        <el-input
            v-model="queryParams.salePrice"
            placeholder="请输入销售价"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="discount">
        <el-input
            v-model="queryParams.discount"
            placeholder="请输入数量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存" prop="stockQuantity">
        <el-input
            v-model="queryParams.stockQuantity"
            placeholder="请输入库存"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存警告水平" prop="stockWarningLevel">
        <el-input
            v-model="queryParams.stockWarningLevel"
            placeholder="请输入库存警告水平"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品的销量" prop="salesVolume">
        <el-input
            v-model="queryParams.salesVolume"
            placeholder="请输入产品的销量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="折扣价" prop="costPrice">
        <el-input
            v-model="queryParams.costPrice"
            placeholder="请输入折扣价"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="重量" prop="weight">
        <el-input
            v-model="queryParams.weight"
            placeholder="请输入重量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大小" prop="size">
        <el-input
            v-model="queryParams.size"
            placeholder="请输入大小"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可配送的地区或国家" prop="deliveryArea">
        <el-input
            v-model="queryParams.deliveryArea"
            placeholder="请输入可配送的地区或国家"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预计到达时间" prop="estimatedArrivalTime">
        <el-date-picker clearable
                        v-model="queryParams.estimatedArrivalTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择预计到达时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="促销活动的开始日期" prop="promotionStartDate">
        <el-date-picker clearable
                        v-model="queryParams.promotionStartDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择促销活动的开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="促销活动的结束日期" prop="promotionEndDate">
        <el-date-picker clearable
                        v-model="queryParams.promotionEndDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择促销活动的结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="促销价格" prop="promotionalPrice">
        <el-input
            v-model="queryParams.promotionalPrice"
            placeholder="请输入促销价格"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createdAt">
        <el-date-picker clearable
                        v-model="queryParams.createdAt"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择创建人">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人" prop="updatedAt">
        <el-date-picker clearable
                        v-model="queryParams.updatedAt"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择更新人">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="产品收到的评价数量" prop="reviewsCount">
        <el-input
            v-model="queryParams.reviewsCount"
            placeholder="请输入产品收到的评价数量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品的平均评分" prop="rating">
        <el-input
            v-model="queryParams.rating"
            placeholder="请输入产品的平均评分"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-input
            v-model="queryParams.supplier"
            placeholder="请输入供应商"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品采购的日期" prop="purchaseDate">
        <el-date-picker clearable
                        v-model="queryParams.purchaseDate"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择产品采购的日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最小订单数量" prop="minOrderQuantity">
        <el-input
            v-model="queryParams.minOrderQuantity"
            placeholder="请输入最小订单数量"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['product:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['product:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['product:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['product:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品id" align="center" prop="productId" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="sku" align="center" prop="sku" />
      <el-table-column label="分类" align="center" prop="category" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="商品图标地址" align="center" prop="imageUrl" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="商品状态" align="center" prop="status" />
      <el-table-column label="商品原价" align="center" prop="originalPrice" />
      <el-table-column label="销售价" align="center" prop="salePrice" />
      <el-table-column label="数量" align="center" prop="discount" />
      <el-table-column label="库存" align="center" prop="stockQuantity" />
      <el-table-column label="库存警告水平" align="center" prop="stockWarningLevel" />
      <el-table-column label="产品的销量" align="center" prop="salesVolume" />
      <el-table-column label="折扣价" align="center" prop="costPrice" />
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="大小" align="center" prop="size" />
      <el-table-column label="可配送的地区或国家" align="center" prop="deliveryArea" />
      <el-table-column label="预计到达时间" align="center" prop="estimatedArrivalTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.estimatedArrivalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="促销类型" align="center" prop="promotionType" />
      <el-table-column label="促销活动的开始日期" align="center" prop="promotionStartDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.promotionStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="促销活动的结束日期" align="center" prop="promotionEndDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.promotionEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="促销价格" align="center" prop="promotionalPrice" />
      <el-table-column label="创建人" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品收到的评价数量" align="center" prop="reviewsCount" />
      <el-table-column label="产品的平均评分" align="center" prop="rating" />
      <el-table-column label="供应商" align="center" prop="supplier" />
      <el-table-column label="产品采购的日期" align="center" prop="purchaseDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.purchaseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最小订单数量" align="center" prop="minOrderQuantity" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['product:product:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['product:product:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改产品对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="productRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="sku" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入sku" />
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-input v-model="form.category" placeholder="请输入分类" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="商品图标地址" prop="imageUrl">
          <el-input v-model="form.imageUrl" placeholder="请输入商品图标地址" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品原价" prop="originalPrice">
          <el-input v-model="form.originalPrice" placeholder="请输入商品原价" />
        </el-form-item>
        <el-form-item label="销售价" prop="salePrice">
          <el-input v-model="form.salePrice" placeholder="请输入销售价" />
        </el-form-item>
        <el-form-item label="数量" prop="discount">
          <el-input v-model="form.discount" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="库存" prop="stockQuantity">
          <el-input v-model="form.stockQuantity" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="库存警告水平" prop="stockWarningLevel">
          <el-input v-model="form.stockWarningLevel" placeholder="请输入库存警告水平" />
        </el-form-item>
        <el-form-item label="产品的销量" prop="salesVolume">
          <el-input v-model="form.salesVolume" placeholder="请输入产品的销量" />
        </el-form-item>
        <el-form-item label="折扣价" prop="costPrice">
          <el-input v-model="form.costPrice" placeholder="请输入折扣价" />
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="大小" prop="size">
          <el-input v-model="form.size" placeholder="请输入大小" />
        </el-form-item>
        <el-form-item label="可配送的地区或国家" prop="deliveryArea">
          <el-input v-model="form.deliveryArea" placeholder="请输入可配送的地区或国家" />
        </el-form-item>
        <el-form-item label="预计到达时间" prop="estimatedArrivalTime">
          <el-date-picker clearable
                          v-model="form.estimatedArrivalTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择预计到达时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="促销活动的开始日期" prop="promotionStartDate">
          <el-date-picker clearable
                          v-model="form.promotionStartDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择促销活动的开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="促销活动的结束日期" prop="promotionEndDate">
          <el-date-picker clearable
                          v-model="form.promotionEndDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择促销活动的结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="促销价格" prop="promotionalPrice">
          <el-input v-model="form.promotionalPrice" placeholder="请输入促销价格" />
        </el-form-item>
        <el-form-item label="创建人" prop="createdAt">
          <el-date-picker clearable
                          v-model="form.createdAt"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择创建人">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新人" prop="updatedAt">
          <el-date-picker clearable
                          v-model="form.updatedAt"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择更新人">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="产品收到的评价数量" prop="reviewsCount">
          <el-input v-model="form.reviewsCount" placeholder="请输入产品收到的评价数量" />
        </el-form-item>
        <el-form-item label="产品的平均评分" prop="rating">
          <el-input v-model="form.rating" placeholder="请输入产品的平均评分" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplier">
          <el-input v-model="form.supplier" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="产品采购的日期" prop="purchaseDate">
          <el-date-picker clearable
                          v-model="form.purchaseDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择产品采购的日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最小订单数量" prop="minOrderQuantity">
          <el-input v-model="form.minOrderQuantity" placeholder="请输入最小订单数量" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Product">
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/product/product";

const { proxy } = getCurrentInstance();

const productList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    productName: null,
    sku: null,
    category: null,
    brand: null,
    imageUrl: null,
    description: null,
    status: null,
    originalPrice: null,
    salePrice: null,
    discount: null,
    stockQuantity: null,
    stockWarningLevel: null,
    salesVolume: null,
    costPrice: null,
    weight: null,
    size: null,
    deliveryArea: null,
    estimatedArrivalTime: null,
    promotionType: null,
    promotionStartDate: null,
    promotionEndDate: null,
    promotionalPrice: null,
    createdAt: null,
    updatedAt: null,
    reviewsCount: null,
    rating: null,
    supplier: null,
    purchaseDate: null,
    minOrderQuantity: null
  },
  rules: {
    productName: [
      { required: true, message: "产品名称不能为空", trigger: "blur" }
    ],
    createdAt: [
      { required: true, message: "创建人不能为空", trigger: "blur" }
    ],
    updatedAt: [
      { required: true, message: "更新人不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询产品列表 */
function getList() {
  loading.value = true;
  listProduct(queryParams.value).then(response => {
    productList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    productId: null,
    productName: null,
    sku: null,
    category: null,
    brand: null,
    imageUrl: null,
    description: null,
    status: null,
    originalPrice: null,
    salePrice: null,
    discount: null,
    stockQuantity: null,
    stockWarningLevel: null,
    salesVolume: null,
    costPrice: null,
    weight: null,
    size: null,
    deliveryArea: null,
    estimatedArrivalTime: null,
    promotionType: null,
    promotionStartDate: null,
    promotionEndDate: null,
    promotionalPrice: null,
    createdAt: null,
    updatedAt: null,
    reviewsCount: null,
    rating: null,
    supplier: null,
    purchaseDate: null,
    minOrderQuantity: null
  };
  proxy.resetForm("productRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.productId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加产品";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _productId = row.productId || ids.value
  getProduct(_productId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改产品";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["productRef"].validate(valid => {
    if (valid) {
      if (form.value.productId != null) {
        updateProduct(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addProduct(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _productIds = row.productId || ids.value;
  proxy.$modal.confirm('是否确认删除产品编号为"' + _productIds + '"的数据项？').then(function() {
    return delProduct(_productIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('product/product/export', {
    ...queryParams.value
  }, `product_${new Date().getTime()}.xlsx`)
}

getList();
</script>
